package org.example.service.impl;

import org.example.mapper.CardInfoMapper;
import org.example.mapper.TradeInfoMapper;
import org.example.pojo.CardInfo;
import org.example.pojo.TradeInfo;
import org.example.service.TradeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class TradeInfoServiceImpl implements TradeInfoService {

    @Autowired
    private CardInfoMapper cardInfoMapper; // 银行卡信息Mapper

    @Autowired
    private TradeInfoMapper tradeInfoMapper; // 交易信息Mapper

    @Transactional
    @Override
    public boolean deposit(TradeInfo tradeInfo) {
        // 1. 根据卡号查询银行卡信息，校验是否存在，是否挂失等
        CardInfo card = cardInfoMapper.selectById(tradeInfo.getCardId());
        if (card == null) {
            throw new RuntimeException("银行卡不存在");
        }
        if (card.getIsReportLoss() != null && card.getIsReportLoss() == 1) {
            throw new RuntimeException("银行卡已挂失，无法存款");
        }
//
//        // 2. 更新银行卡余额 = 旧余额 + 存款金额
//        BigDecimal newBalance = card.getBalance().add(tradeInfo.getTradeMoney());
//        card.setBalance(newBalance);
//        cardInfoMapper.updateById(card);

        // 3. 插入交易信息（tradeInfo）
        tradeInfo.setTradeType("存款");
        tradeInfo.setTradeDate(new Date());
        tradeInfoMapper.insert(tradeInfo);

        return true;
    }

    @Transactional
    @Override
    public boolean withdraw(String cardId, BigDecimal amount) {


        CardInfo card = cardInfoMapper.selectById(cardId);
        if (card == null) {
            throw new RuntimeException("银行卡不存在");
        }
        if (card.getIsReportLoss() != null && card.getIsReportLoss()  == 1) {
            throw new RuntimeException("银行卡已挂失，无法存款");
        }
        // 这里可加余额校验逻辑，防止透支
        // 假设有方法 getBalanceByCardId
        BigDecimal balance = card.getBalance();
        if (balance.compareTo(amount) < 0) {
            throw new RuntimeException("余额不足");
        }

        TradeInfo tradeInfo = new TradeInfo();
        tradeInfo.setCardId(cardId);
        tradeInfo.setTradeType("取款");
        tradeInfo.setTradeMoney(amount);
        tradeInfo.setTradeDate(new Date());
        tradeInfo.setRemark("客户取款");

        tradeInfoMapper.insert(tradeInfo);
        return true;
    }

    // 查询余额示例（需在Mapper写对应SQL）

    @Transactional
    public void transfer(String fromCardId, String toCardId, BigDecimal amount) {
        if (fromCardId.equals(toCardId)) {
            throw new RuntimeException("不能给自己转账");
        }
        CardInfo card = cardInfoMapper.selectById(fromCardId);
        BigDecimal balance = card.getBalance();
        if (balance == null || balance.compareTo(amount) < 0) {
            throw new RuntimeException("余额不足");
        }

        // 插入转出记录
        TradeInfo out = new TradeInfo();
        out.setCardId(fromCardId);
        out.setTradeType("转出");
        out.setTradeMoney(amount);
        out.setTradeDate(new Date());
        out.setRemark("转账到卡号：" + toCardId);
        tradeInfoMapper.insert(out);

        // 插入转入记录
        TradeInfo in = new TradeInfo();
        in.setCardId(toCardId);
        in.setTradeType("转入");
        in.setTradeMoney(amount);
        in.setTradeDate(new Date());
        in.setRemark("来自卡号：" + fromCardId);
        tradeInfoMapper.insert(in);

        // 余额由数据库触发器自动维护，无需手动更新 balance 字段
    }



}